package com.acme.domain.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.acme.domain.account.Account;

public class Client implements Serializable {

	private String name;
	private Gender gender;

	private List<Account> accounts = new ArrayList<Account>();

	public Client(final String name, final Gender gender) {
		this.name = name;
		this.gender = gender;
	}

	public String getName() {
		return name;
	}

	public String getSalutation() {
		if (gender != null) {
			return gender.getSalutation() + " " + name;
		} else {
			return name;
		}
	}

	public Account getAccount(final int id) {
		for (Account acc : accounts) {
			if (acc.getAccountNumber() == id) {
				return acc;
			}
		}
		return null;
	}

	public void addAccount(final Account account) {
		accounts.add(account);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("***********\n");
		sb.append("Client: ");
		sb.append(this.name + "\n");
		
		sb.append("Accounts:\n");
		sb.append(getAccountsListInfo());
		
		sb.append("***********");
		return sb.toString();
	}
	
	private String getAccountsListInfo() {
		List<Account> accountsList = this.getAccountsList();
		StringBuffer sb = new StringBuffer();
		for(Account acc: accountsList) {
			sb.append("Account #" + acc.getAccountNumber() + ": ");
			sb.append("balance: " + acc.getBalance());
			sb.append("\n");
		}
		return sb.toString();
	}

	public List<Account> getAccountsList() {
		return Collections.unmodifiableList(accounts);
	}

	public boolean Equals(Object obj) {
		if(obj instanceof Client) {
			Client cl = (Client)obj;
			return cl.getName() == this.name && cl.gender == this.gender;
		}
		return false;
	}
	
	public int hashCode() {
		return this.name.hashCode();
	}
}
